<template>
  <div class="modal-overlay" v-if="visible">
    <div class="modal-content questionnaire-modal">
      <div class="modal-header">
        <div class="modal-icon">
          <i class="fas fa-clipboard-check"></i>
        </div>
        <h2 class="modal-title">健康问卷 - {{ department }}（{{ doctor }}医生）</h2>
        <button type="button" class="btn-close" @click="closeModal">
          <i class="fas fa-times"></i>
        </button>
      </div>
      <div class="modal-body">
        <div
          class="question-item"
          v-for="(question, index) in questions"
          :key="index"
        >
          <div class="question-text">{{ index + 1 }}. {{ question.text }}</div>
          <div class="options-container">
            <div
              class="option-item"
              v-for="(option, optIndex) in question.options"
              :key="optIndex"
              :class="{ selected: modelValue[index] === option }"
              @click="selectOption(index, option)"
            >
              <div class="option-radio">
                <div class="radio-dot" v-if="modelValue[index] === option"></div>
              </div>
              <div class="option-text">{{ option }}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" @click="skipQuestionnaire">
          <i class="fas fa-forward"></i> 跳过问卷
        </button>
        <button type="button" class="btn btn-outline" @click="closeModal">
          <i class="fas fa-times"></i> 取消
        </button>
        <button
          type="button"
          class="btn btn-primary"
          @click="submitQuestionnaire"
          :disabled="!isQuestionnaireFilled"
        >
          <i class="fas fa-check"></i> 提交问卷
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    required: true
  },
  department: {
    type: String,
    required: true
  },
  doctor: {
    type: String,
    required: true
  },
  questions: {
    type: Array,
    required: true
  },
  modelValue: {
    type: Array,
    required: true
  }
})

const emit = defineEmits(['update:modelValue', 'close', 'submit', 'skip'])

const isQuestionnaireFilled = computed(() => {
  return props.modelValue.every(ans => ans && ans.trim() !== '')
})

const selectOption = (questionIndex, option) => {
  const newAnswers = [...props.modelValue]
  newAnswers[questionIndex] = option
  emit('update:modelValue', newAnswers)
}

const closeModal = () => {
  emit('close')
}

const submitQuestionnaire = () => {
  if (!isQuestionnaireFilled.value) {
    alert('请完成所有问题后再提交！')
    return
  }
  emit('submit')
}

const skipQuestionnaire = () => {
  emit('skip')
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.modal-content {
  background-color: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  max-width: 700px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  padding: 25px;
  border-bottom: 2px solid #f0f0f0;
  position: relative;
}

.modal-icon {
  font-size: 28px;
  color: #3498db;
  margin-right: 15px;
}

.modal-title {
  font-size: 24px;
  font-weight: 700;
  color: #2980b9;
  margin: 0;
  flex: 1;
}

.btn-close {
  background: none;
  border: none;
  font-size: 24px;
  color: #777;
  cursor: pointer;
  padding: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
  border-radius: 50%;
  width: 40px;
  height: 40px;
}

.btn-close:hover {
  color: #333;
  background-color: #f0f0f0;
  transform: scale(1.1);
}

.modal-body {
  padding: 25px;
}

.question-item {
  margin-bottom: 30px;
}

.question-text {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 15px;
  color: #333;
}

.options-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.option-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s;
}

.option-item:hover {
  border-color: #3498db;
  background-color: rgba(52, 152, 219, 0.05);
  transform: translateY(-2px);
}

.option-item.selected {
  border-color: #3498db;
  background-color: rgba(52, 152, 219, 0.1);
}

.option-radio {
  width: 24px;
  height: 24px;
  border: 2px solid #ddd;
  border-radius: 50%;
  margin-right: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}

.option-item.selected .option-radio {
  border-color: #3498db;
}

.radio-dot {
  width: 12px;
  height: 12px;
  background-color: #3498db;
  border-radius: 50%;
}

.option-text {
  font-size: 18px;
  color: #333;
}

.modal-footer {
  padding: 20px 25px;
  border-top: 2px solid #f0f0f0;
  display: flex;
  justify-content: flex-end;
  gap: 15px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  min-width: 120px;
}

.btn i {
  margin-right: 8px;
}

.btn-primary {
  background-color: #3498db;
  color: white;
}

.btn-primary:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.btn-primary:disabled {
  background-color: #bdc3c7 !important;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-secondary {
  background-color: #f39c12;
  color: white;
}

.btn-secondary:hover {
  background-color: #e67e22;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.btn-outline {
  background-color: #ffffff;
  color: #777;
  border: 2px solid #ddd;
}

.btn-outline:hover {
  background-color: #f0f0f0;
  border-color: #ccc;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .modal-content {
    margin: 10px;
  }
  
  .modal-header {
    padding: 20px 15px;
  }
  
  .modal-title {
    font-size: 20px;
  }
  
  .modal-body {
    padding: 20px 15px;
  }
  
  .question-text {
    font-size: 18px;
  }
  
  .option-text {
    font-size: 16px;
  }
  
  .modal-footer {
    flex-direction: column;
  }
  
  .modal-footer .btn {
    width: 100%;
  }
}
</style>